#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-12-17 9:15 上午
# @Author  : yangdy
# @File    : 2020-12-17.714.买卖股票的最佳时机含手续费
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        # 1. 0没有持有啥也不做，1没有持有买入，2持有啥也不做(初始-prices[0])，3持有卖出
        # dp = [[0, -prices[0], -prices[0], 0]]
        # for i in range(1, len(prices)):
        #     p = prices[i]
        #     data = [
        #         max(dp[-1][0], dp[-1][3]),
        #         max(dp[-1][0]-p, dp[-1][3]-p),
        #         max(dp[-1][1], dp[-1][2]),
        #         max(dp[-1][1]+p-fee, dp[-1][2]+p-fee)
        #     ]
        #     dp.append(data)
        # return max(dp[-1])
        # 2. 0没有持有，1持有
        # dp = [[0, -prices[0]]]
        # for p in prices[1:]:
        #     dp.append([
        #         max(dp[-1][0], dp[-1][1]+p-fee),
        #         max(dp[-1][1], dp[-1][0]-p)
        #     ])
        # return dp[-1][0]
        a, b = 0, -prices[0]
        for p in prices[1:]:
            a, b = max(a, b+p-fee), max(b, a-p)
        return a


questions = [
    ([1, 3, 2, 8, 4, 9], 2),
    ([1, 3, 7, 5, 10, 3], 3),
]

work(questions, lambda q: Solution().maxProfit(q[0], q[1]))
